/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.scuecsz.view.mbean.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.faces.model.SelectItem;
import org.scuecsz.view.utils.FaceUtil;

/**
 *
 * @author Administrator
 */
public class CommonMbean implements Serializable {

    /** Creates a new instance of MainContant */
    private static String root;
    private static List years;
    private static List months;

    public CommonMbean() {
        root = FaceUtil.getServletContext().getContextPath();
    }

    public String getRoot() {
        return root;
    }

    public synchronized void yearsInit(int yearNum) {
        if (years == null) {
            years = new ArrayList();
            SelectItem item=null;
            for (int i = yearNum; i >= 1951; i--) {
                item = new SelectItem(String.valueOf(i));
                years.add(item);
            }
        } else {
            SelectItem item = (SelectItem) years.get(0);
            int lastYear = Integer.parseInt(String.valueOf(item.getValue()));
            if (yearNum > lastYear) {
                    years.clear();
             for (int i = yearNum; i >= 1951; i--) {
                item = new SelectItem(String.valueOf(i));
                years.add(item);
             }
            }
        }
    }

    public synchronized void monthsInit() {
        if (months == null) {
            months = new ArrayList();
            for (int i = 1; i <= 12; i++) {
                SelectItem item = new SelectItem(String.valueOf(i));
                months.add(item);
            }
        }
    }

    public synchronized void daysInit(int year, int month) {
        Calendar c = Calendar.getInstance();
        c.set(year, month - 1, 1);
        int num = c.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public List getYears() {
        yearsInit(Calendar.getInstance().getTime().getYear() + 1900);
        return years;
    }

    public void setYears(List years) {
        CommonMbean.years = years;
    }

    public static List getMonths() {
        return months;
    }

    public static void setMonths(List months) {
        CommonMbean.months = months;
    }


}
